package com.ellricky.tc_manager.ui.home

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.andr.common.permission.GrantResult
import com.andr.common.permission.PermissionManager
import com.andr.common.permission.Permissions
import com.andr.common.ui.fragment.BaseToolBarFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.adapter.course.JobTrainingActivity
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.ui.ActiveDocActivity
import com.ellricky.baselib.ui.ErCodeActivity
import com.ellricky.baselib.ui.cla.ClassManageFragment
import com.ellricky.baselib.ui.order.RenewalManageActivity
import com.ellricky.baselib.ui.visit.VisitListActivity2
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.jump
import com.ellricky.tc_manager.R
import com.google.zxing.QrCodeUtil
import com.google.zxing.QrConstant
import kotlinx.android.synthetic.main.tm_fragment_home.*

class TeachManageMainFragment :
    BaseToolBarFragment<Void, BasePresenter<Void>>()/*, RvRefreshListener*/ {

    private lateinit var classManageFragment: ClassManageFragment

    companion object {
        fun newInstance(): TeachManageMainFragment {
            val fragment = TeachManageMainFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun getContentLayout(): Int = R.layout.tm_fragment_home

    override fun initUIView(root: View) {
        super.initUIView(root)
        mTvTitle.setText(R.string.base_home)
        setLeftIb(R.drawable.base_icon_ercode)
        setRightBtn(R.drawable.tm_icon_scan)
        tvReserve.setOnClickListener(this)
        tvSign.setOnClickListener(this)
        tvApply.setOnClickListener(this)
        tvAm.setOnClickListener(this)
        tvOrder.setOnClickListener(this)
        tvRenewal.setOnClickListener(this)

        classManageFragment = ClassManageFragment.newInstance(true)
        childFragmentManager.beginTransaction()
            .replace(R.id.frgContainer, classManageFragment)
            .commit()
    }

    override fun onResume() {
        super.onResume()
        val load = LocalCacheUtil.isReloadData()
        if (load) classManageFragment.loadClassRooms()
        LocalCacheUtil.setIsReloadData(true)
    }

    override fun onClickView(view: View) {
        super.onClickView(view)
        when (view) {
            mIbBack -> EAppUtil.jumpPageWithNoParam(this, ErCodeActivity::class.java)
            mBtnRight -> openScan()

            tvReserve -> EAppUtil.jumpPageWithNoParam(this, JobTrainingActivity::class.java)
            tvSign -> EAppUtil.jumpClassManage(this)
            tvApply -> EAppUtil.jumpPageWithNoParam(this, ActiveDocActivity::class.java)
            tvAm -> EAppUtil.jumpPageWithNoParam(this, VisitListActivity2::class.java)
            tvOrder -> EAppUtil.jumpCommonFragment(
                context,
                BaseConstant.FRAGMENT_ORDER_MANAGER,
                null
            )
            tvRenewal -> jump<RenewalManageActivity>()
        }
    }

///////////////////////////////////////////////////////////////////////////


    private fun openScan() {
        // 拨打电话
        PermissionManager.getInstance()
            .setGrantResult(object : GrantResult {
                override fun onGrantedAll() {
                    QrCodeUtil.jumpScan(this@TeachManageMainFragment)
                }

                override fun onDenied(vararg permission: String?) {
                    when (permission[0]) {
                        Permissions.CAMERA ->
                            ToastUtil.show(mContext, getString(R.string.open_camera_permission_tip))
                    }
                }

                override fun onExplain(vararg permission: String?) {
                }

            })
            .requestPermissions(mContext, Permissions.CAMERA)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //扫描结果回调
        if (resultCode == QrConstant.SCAN_RESULT_OK) { //RESULT_OK = -1
            val bundle = data?.extras
            val scanResult = bundle?.getString("qr_scan_result") ?: ""
            //将扫描出的信息显示出来
//            postScanResult(scanResult)
        }
    }
}
